package com.kiln.juxingqs.fragment

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.base.BaseFragment
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.WdjpAdapter
import com.kiln.juxingqs.api.bean.GoodsOrder
import com.kiln.juxingqs.databinding.FragmentWdjponeBinding
import com.kiln.juxingqs.dialog.MsgTitleDialog
import com.kiln.juxingqs.ui.vm.WdjpViewModel

class WdjpThreeFragment : BaseFragment<FragmentWdjponeBinding, WdjpViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_wdjpone
    }

    val adapter: WdjpAdapter by lazy {
        WdjpAdapter(requireContext()).apply {
            cancle = {
                MsgTitleDialog(requireContext()).apply {
                    title = "撤销订单？"
                    okTxt = "确认"
                    cancleTxt = "关闭"
                    ok = {
                        viewModel.cancle(it)
                    }
                }.show()
            }
        }
    }

    override fun initViewModel(): WdjpViewModel = createViewModel()

    override fun initWidget() {

        binding.emptyLayout.setOnClickListener {
            viewModel.pageThree.value = 0
            viewModel.holdThree()
        }
        val decoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)

        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isThreeLoading.value!!) {
                viewModel.holdThree()
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true

            viewModel.pageThree.value = 0
            viewModel.holdThree()
        }
        viewModel.isThreeLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callThree.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.order_list as MutableList<GoodsOrder.Order?>?
                    if (viewModel.pageThree.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

    override fun initData() {
        viewModel.pageThree.value = 0
        viewModel.holdThree()
    }

}